#include "tmaudioworker.h"
#include "tmglobalctrl.h"

TMAudioWorker::TMAudioWorker(QObject *parent) : QObject(parent)
{
    pr_Timer = new QTimer();
    connect(pr_Timer,SIGNAL(timeout()),this,SLOT(slot_ttsState()));
//    pr_Timer->start(500);
}

void TMAudioWorker::slot_ttsState()
{
    try{
        bool m_bIsPlaying = m_prxAudio->TtsIsPlaying();
        emit sig_ttsState(m_bIsPlaying);
        bool m_bMediaState = m_prxAudio->MediaIsPlaying();
        emit sig_mediaState(m_bMediaState);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_ttsPlay(QString str_tts)
{
    try{
        m_prxAudio->TtsPlay(str_tts.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_ttsPlayParams(QString str_tts)
{
    try{
        m_prxAudio->TtsPlayParams(str_tts.toUtf8().toStdString(),"xiaoyan",67,70);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_ttsPlayFile(QString filePath)
{

    try{
        m_prxAudio->TtsPlayFile(filePath.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_ttsSetVolume(int nVolume)
{
    try{
        m_prxAudio->TtsSetVolume(nVolume);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_ttsStop()
{
    try{
        m_prxAudio->TtsStop();
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_asrBuild(QString filePath)
{

    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)){
        return;
    }
    std::vector<std::string> strList;
    QByteArray line;
    // 这里要发送：UTF-8 编码的std::string
    while ( !file.atEnd() ) {
        line = file.readLine(); // 不包括“\n”的一行文本
//        qDebug()<<n++<<line;
        line = line.replace("\r","");
        line = line.replace("\n","");
        strList.push_back(line.toStdString());
    }
    file.close();
    try{
        m_prxAudio->AsrBuildCmd(strList);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_asrStart(int nMaxTime)
{
    try{
        m_prxAudio->AsrStart(nMaxTime);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_asrStop()
{
    try{
        m_prxAudio->AsrStop();
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_mediaPlay(QString filePath)
{

    try{
        m_prxAudio->MediaPlay(filePath.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_mediaVolume(int nVolume)
{
    try{
        m_prxAudio->MediaSetVolume(nVolume);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMAudioWorker::slot_mediaStop()
{
    try{
        m_prxAudio->MediaStop();
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}
